import 'package:flutter/material.dart';
import 'package:forui/forui.dart';
import 'package:get/get.dart';
import 'package:home_box/pages/medicine/MedicineM/medicine_model.dart';
import 'package:home_box/pages/medicine/MedicineVM/medicine_add_vm.dart';

import '../../../../constants.dart';

///药品用法
class MedicineTarget extends StatelessWidget {
  const MedicineTarget({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MedicineVM>(
      tag: 'MedicineVM',
      builder: (controller) {
        return Container(
          margin: EdgeInsets.only(top: 10),
          child: GestureDetector(
            onTap: () {
              _showDailyFrequency(context, controller);
            },
            child: Row(
              children: [
                Text('使用人群：'),
                Text(
                  controller.medicine.value.target == Target.adult
                      ? '成人'
                      : '小孩',
                  style: TextStyle(fontSize: 14, color: Colors.grey),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

///每天用法弹窗
Future<int?> _showDailyFrequency(BuildContext context, MedicineVM vm) {

  FPickerController controller = FPickerController(
    initialIndexes: [vm.targetIndex.value],
  );
  return showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            SizedBox(
              height: 60,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    left: 15,
                    child: SizedBox(
                      width: 15,
                      height: 15,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Image.asset('images/close.png'),
                      ),
                    ),
                  ),
                  Positioned(
                    right: 10,
                    child: GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                        int index = controller.value[0];
                        vm.targetIndex.value = index;
                        vm.updateTarget(
                          index == 0 ? Target.adult : Target.child,
                        );
                        print(vm.medicine.value.target);
                      },
                      child: Container(
                        margin: EdgeInsets.all(20.0),
                        child: Text('确认', style: TextStyle(color: Colors.blue)),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Center(
              child: SizedBox(
                width: 200,
                height: 300,
                child: FPicker(
                  controller: controller,
                  children: [
                    FPickerWheel(
                      flex: 1,
                      loop: false,
                      children:
                          Constants.target.map((value) {
                            return Text(value);
                          }).toList(),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
    },
  );
}
